﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;
using RATSharp.Model;
using Microsoft.Phone.Shell;
using System.Diagnostics;
using RATSharpW7.ViewModel.CalendarViewModel;

namespace RATSharpW7.View
{
    /// <summary>
    /// Day screen. 
    /// </summary>
    public partial class Day : PhoneApplicationPage
    {
        bool isNewPageInstance = false;
        public AbstractDayViewModel model { get; set; }
        ApplicationBarIconButton createNewReservationButton;
        //provides colors and actions
        ICalendarDataProvider provider;
        public Day()
        {
            InitializeComponent();
            isNewPageInstance = true;
        }

        private void periodsListbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                // get item from ListBox and store in class var. Store page to navigate to in class var.
                RATSharp.Model.period selectedItem = (RATSharp.Model.period)e.AddedItems[0];
                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // PageTransition.Begin();

                if (model.isPeriodEditable(selectedItem))
                {
                    NavigationService.Navigate("View", typeof(EditReservation), selectedItem);
                }
                else
                {
                    NavigationService.Navigate("View", typeof(ViewReservation), selectedItem);
                }
            }
        }

        private void createNewReservationButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/ObjectBrowser.xaml", UriKind.Relative));
        }

        private void initActionBar()
        {
            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.Opacity = 1;
            createNewReservationButton = new ApplicationBarIconButton(new Uri("/Images/addreservation.png", UriKind.RelativeOrAbsolute));
            createNewReservationButton.Text = Application.Current.Resources["AddReservationLabel"] as string;
            createNewReservationButton.Click += new EventHandler(createNewReservationButton_Click);
            createNewReservationButton.IsEnabled = DateTime.Compare(model.SelectedDate, DateTime.Now.Date) >= 0;
            this.ApplicationBar.Buttons.Add(createNewReservationButton); 
        }


        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationMode !=
                  System.Windows.Navigation.NavigationMode.Back)
            {
                State["DayViewModel"] = model;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("DayViewModel"))
                    {
                        MessageBox.Show("Retrieving state...");
                        model = State["DayViewModel"]
                                           as AbstractDayViewModel;
                    }
                    else
                    {
                        Calendar.DayViewData data = NavigationContext.GetData<Calendar.DayViewData>();
                        DateTime selectedDate = data.selectedDate;
                        provider = data.dataProvider;
                       
                        model = provider.getDayModel(selectedDate);
                        
                    }
                }
            }
            else 
            {
                RATSharp.Model.Object selObject = ObjectBrowser.SelectedObject;
                if (selObject != null)
                {
                    RATSharp.Model.ReservationFactory.CreateReservationData data = new RATSharp.Model.ReservationFactory.CreateReservationData();
                    data.obj = selObject;
                    data.selectedDate = model.SelectedDate;
                    data.pickAction = PickAction.NOT_PICKED;
                    NavigationService.Navigate("View", typeof(CreateReservation), data);
                    isNewPageInstance = false;
                    return;
                }
            }
            DataContext = model;
            initActionBar();
            //just to be sure reload all events every time when screen is materialized.
            //user could create a new reservation...
            model.loadPeriods();
            isNewPageInstance = false;
        }
    }
}